// 首页逻辑
Page({
  data: {
    // 总资产数据
    totalGold: 156.89,
    changeType: 'up', // 'up' 或 'down'
    changeAmount: 2.34,
    
    // 月度目标数据
    currentMonthGold: 28.5,
    monthlyGoal: 50,
    progressPercent: 57, // 计算得出 (28.5 / 50) * 100 = 57
    
    // 攒金计划列表
    plans: [
      {
        id: 1,
        name: "每日攒金",
        desc: "每天自动攒0.5g",
        amount: 36.5,
        type: "regular", // "regular" 定期, "flexible" 灵活
        status: "active" // "active" 进行中, "completed" 已完成
      },
      {
        id: 2,
        name: "黄金定投",
        desc: "每月1号攒5g",
        amount: 25.0,
        type: "regular",
        status: "active"
      },
      {
        id: 3,
        name: "应急储备",
        desc: "灵活攒金，随时可取",
        amount: 95.39,
        type: "flexible",
        status: "active"
      }
    ],
    
    // 活动列表
    activities: [
      {
        id: 1,
        title: "新用户专享，首次攒金送0.1g",
        image: "/images/activity1.jpg",
        time: "2023-10-01至2023-10-31"
      },
      {
        id: 2,
        title: "邀请好友，双方各得0.5g黄金",
        image: "/images/activity2.jpg",
        time: "长期有效"
      }
    ]
  },
  
  onLoad() {
    // 页面加载时的初始化操作
    this.calculateProgress();
  },
  
  // 计算进度百分比
  calculateProgress() {
    const { currentMonthGold, monthlyGoal } = this.data;
    const percent = Math.min(Math.round((currentMonthGold / monthlyGoal) * 100), 100);
    this.setData({
      progressPercent: percent
    });
  },
  
  // 跳转到个人中心
  goToProfile() {
    wx.navigateTo({
      url: '/pages/profile/profile'
    });
  },
  
  // 立即攒金
  saveGold() {
    wx.navigateTo({
      url: '/pages/save-gold/save-gold'
    });
  },
  
  // 兑换提现
  redeemGold() {
    wx.navigateTo({
      url: '/pages/redeem/redeem'
    });
  },
  
  // 查看所有目标
  viewGoals() {
    wx.navigateTo({
      url: '/pages/goals/goals'
    });
  },
  
  // 新增计划
  addPlan() {
    wx.navigateTo({
      url: '/pages/add-plan/add-plan'
    });
  },
  
  // 查看计划详情
  viewPlanDetail(e) {
    const planId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/plan-detail/plan-detail?id=${planId}`
    });
  },
  
  // 查看所有活动
  viewAllActivities() {
    wx.navigateTo({
      url: '/pages/activities/activities'
    });
  },
  
  // 查看活动详情
  viewActivity(e) {
    const activityId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/activity-detail/activity-detail?id=${activityId}`
    });
  },
  
  // 切换底部导航
  switchTab(e) {
    const tab = e.currentTarget.dataset.tab;
    switch(tab) {
      case 'home':
        // 已经在首页，无需操作
        break;
      case 'plans':
        wx.redirectTo({
          url: '/pages/plans/plans'
        });
        break;
      case 'market':
        wx.redirectTo({
          url: '/pages/market/market'
        });
        break;
      case 'mine':
        wx.redirectTo({
          url: '/pages/mine/mine'
        });
        break;
    }
  }
});
